package com.psq.core.common.base

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ProgressBar
import androidx.fragment.app.Fragment
import cn.wufuqi.convertmainthread.ConvertMainThread
import com.psq.core.common.R
import com.saicmotor.core.common.utils.isNotNull
import com.saicmotor.core.common.utils.isNull

abstract class BaseFragment  : Fragment(), IPattern{

    var handler: Handler? = null

    /**
     * 根view
     */
    lateinit var mRootView: View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        handler = Handler(Looper.getMainLooper())
        doBeforeView(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mRootView = inflater.inflate(getLayoutResId(), container, false)
        return mRootView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(savedInstanceState)
        doAfterView(savedInstanceState)
    }



    override fun initStatusBar() {

    }

    override fun doBeforeView(savedInstanceState: Bundle?) {

    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun doAfterView(savedInstanceState: Bundle?) {

    }

    open val mProgressBar: ProgressBar?
        get() {
            val progressBar = mRootView.findViewById<ProgressBar?>(R.id.progress_bar)
            return if (progressBar.isNull()) {
                if (context.isNotNull())
                    LayoutInflater.from(context)
                        .inflate(R.layout.layout_progressbar, mRootView as? ViewGroup, true)
                        .findViewById<ProgressBar?>(R.id.progress_bar)
                else null
            } else progressBar
        }

    override fun showProgressBar() {
        if (!isProgressBarCancelable())
            activity?.window?.setFlags(
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
            )
        mProgressBar?.visibility = View.VISIBLE
    }

    override fun hideProgressBar() {
        if (!isProgressBarCancelable())
            activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        mProgressBar?.visibility = View.GONE
    }

    override fun isProgressBarCancelable() = true

    /**
     * 切换到主线程
     */
    override fun runOnUiThread(action: Runnable) {
        ConvertMainThread.runOnUiThread { action.run() }
    }


    override fun runLifecycleOnUiThread(action: Runnable) {
        if (Thread.currentThread() !== Looper.getMainLooper().thread) {
            handler?.post(action)
        } else {
            handler?.let {
                action.run()
            }
        }
    }
}